package com.lianzai.mobile.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lianzai.common.executor.LocalSvrSyncExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lianzai.mobile.dao.CommunityInfoDao;
import com.lianzai.mobile.domain.CommunityInfoDO;
import com.lianzai.mobile.service.CommunityInfoService;
import org.springframework.web.client.RestTemplate;


@Service
public class CommunityInfoServiceImpl implements CommunityInfoService {
	@Autowired
	private CommunityInfoDao communityInfoDao;

	@Override
	public CommunityInfoDO get(Integer id){
		return communityInfoDao.get(id);
	}

	@Override
	public List<CommunityInfoDO> list(Map<String, Object> map){
		return communityInfoDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return communityInfoDao.count(map);
	}

	@Override
	public int save(CommunityInfoDO communityInfo){
		communityInfo.setCreateTime(new Date());
		communityInfo.setUpdateTime(new Date());
		int ret = communityInfoDao.save(communityInfo);

		if (ret > 0)
		{
			//向本地系统同步
			LocalSvrSyncExecutor.addCommunity(communityInfo, this);
		}
		return ret;
	}

	@Override
	public int update(CommunityInfoDO communityInfo){
		communityInfo.setUpdateTime(new Date());
		int ret = communityInfoDao.update(communityInfo);

		if (ret > 0)
		{
			//向本地系统同步
			LocalSvrSyncExecutor.updateCommunity(communityInfo,this);
		}

		return ret;
	}

	@Override
	public int updateSyncStatus(CommunityInfoDO communityInfo)
	{
		return communityInfoDao.updateSyncStatus(communityInfo);
	}

	@Override
	public int syncToLocalSystem(Integer communityId) {

		CommunityInfoDO communityInfo = communityInfoDao.get(communityId);
		LocalSvrSyncExecutor.addCommunity(communityInfo,this);

		return 1;
	}

	@Override
	public int remove(Integer id){

		CommunityInfoDO communityInfo = communityInfoDao.get(id);
		int ret = communityInfoDao.remove(id);

		//同步到本地系统
		if (ret > 0 && communityInfo != null)
		{
			//向本地系统同步LocalSvrSyncExecutor
			LocalSvrSyncExecutor.delCommunity(communityInfo);
		}

		return ret;
	}

	@Override
	public int batchRemove(Integer[] ids){

		int ret = communityInfoDao.batchRemove(ids);

		for (int id : ids)
		{
			//同步到本地系统
			CommunityInfoDO communityInfo = communityInfoDao.get(id);
			String url = "http://" + communityInfo.getLocalServerIp() + ":"
					+ communityInfo.getLocalServerPort() + "/remoteManage/delCommunity";

			JSONObject json = new JSONObject();
			json.put("id", id);
			RestTemplate restTemplate = new RestTemplate();
			restTemplate.postForObject( url, json, JSONObject.class);
		}

		return ret;
	}


	public static void main(String[] args)
	{
		System.out.println("begin");

		new Thread() {
			public void run() {
				try {
					Thread.sleep(10000);
				} catch (Exception e) {}
			}
		}.start();

		System.out.println("end");
	}

	/**
	 * 本地绑定小区操作
	 */
	@Override
	public int updateCommunityRule(Map<String, Object> map){
		return communityInfoDao.updateCommunityRule(map);
	}
}
